﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.CRM;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdateShop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)//should be LEVEL_02 ???
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();
            dba.AddTable(DCompany.TABLE);
            dba.AddColumn(DCompany.CODE);
            dba.AddColumn(DCompany.NAME);
            var dt = dba.ExecuteSelect();
            var cnt = dt.Rows.Count;

            string name;
            string code;
            for (int i = 0; i < cnt; i += 1)
            {
                name = dt.Rows[i][DCompany.NAME] as string;
                code = dt.Rows[i][DCompany.CODE] as string;
                LbCompany.Items.Add(new ListItem { Text = name, Value = code });
                CbCompany.Items.Add(new ListItem { Text = name, Value = code });
            }

            CbLevel.Items.Add(new ListItem { Text = "旗舰店", Value = CShop.LEVEL_0.ToString() });
            CbLevel.Items.Add(new ListItem { Text = "一级门店", Value = CShop.LEVEL_1.ToString() });
            CbLevel.Items.Add(new ListItem { Text = "二级门店", Value = CShop.LEVEL_2.ToString() });
            CbLevel.Items.Add(new ListItem { Text = "三级门店", Value = CShop.LEVEL_3.ToString() });
            CbLevel.Items.Add(new ListItem { Text = "四级门店", Value = CShop.LEVEL_4.ToString() });

            TbAddr.MaxLength = CShop.ADDRESS;
            TbShopName.MaxLength = CShop.SHOP_NAME;
            TbCellPhone.MaxLength = CShop.CELLPHONE;
            TbTelephone.MaxLength = CShop.TELEPHONE;

            var corp = Request["corp"];
            if (!CharUtil.IsValidateCode(corp))
            {
                corp = "0";
            }
            LbCompany.SelectedValue = corp;
            CbCompany.SelectedValue = corp;

            var shop = Request["shop"];
            if (CharUtil.IsValidateHash(shop))
            {
                dba.Reset();
                ReadShop(dba, corp, shop);
            }
        }

        #region 事件处理
        protected void BtUpdateShop_Click(object sender, EventArgs e)
        {
            SaveShop();
        }

        protected void BtCancelShop_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/Shops.aspx?corp=" + LbCompany.SelectedValue);
        }
        #endregion

        private void ReadShop(RDBEngine dba, string corp, string shop)
        {
            dba.AddTable(DShop.TABLE);
            dba.AddWhere(DShop.CORP_ID, corp);
            dba.AddWhere(DShop.ID, shop);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HdId.Value = shop;
            TbCode.Text = "" + row[DShop.CODE];
            TbShopName.Text = "" + row[DShop.NAME];
            TbAddr.Text = "" + row[DShop.ADDRESS];
            CbCompany.SelectedValue = "" + row[DShop.CORP_ID];
            CbLevel.SelectedValue = "" + row[DShop.LEVEL];
            TbPostcode.Text = "" + row[DShop.POSTCODE];
            TbCellPhone.Text = "" + row[DShop.CELLPHONE];
            TbTelephone.Text = "" + row[DShop.TELEPHONE];
            TbContact.Text = "" + row[DShop.CONTACT];
            CkStatus.Checked = (CShop.STATUS_DISABLED == (int)row[DShop.STATUS]);
        }

        private void SaveShop()
        {
            LlErr.Text = "";
            var id = HdId.Value;


            var shopName = TbShopName.Text.Trim();
            if (!WebUtil.IsValid(shopName, "门店名称", CShop.SHOP_NAME, LlErr))
            {
                return;
            }

            var shopAddress = TbAddr.Text.Trim();
            if (!WebUtil.IsValid(shopAddress, "门店地址", CShop.ADDRESS, LlErr))
            {
                return;
            }

            var contact = TbContact.Text.Trim();
            if (!WebUtil.IsValid(contact, "联系人", CShop.CONTACT, LlErr))
            {
                return;
            }

            #region 检测门店的重复性
            var dba = new RDBEngine();
            dba.AddTable(DShop.TABLE);
            dba.AddColumn(DShop.NAME);
            dba.AddWhere(DShop.NAME, shopName);
            dba.AddWhere(DShop.CORP_ID, CbCompany.SelectedValue);
            dba.AddWhere(DShop.STATUS, CShop.STATUE_ENABLED.ToString(), false);
            if (CharUtil.IsValidateHash(id))
            {
                dba.AddWhere(DShop.ID, "!=", id, true);
            }
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 0)
            {
                LlErr.Text = string.Format("在 \"{0}\" 公司中已存在名为 \"{1}\" 的门店！", CbCompany.SelectedItem.Text, shopName);
                return;
            }
            #endregion

            var corp = Request["corp"];
            if (!CharUtil.IsValidateCode(corp))
            {
                corp = "0";
            }

            bool isUpdate = CharUtil.IsValidateHash(id);
            string code = "";
            if (!isUpdate)
            {
                dba.Reset();
                code = KeyUtil.NextShopKey(dba, corp);
            }

            dba.Reset();
            dba.AddTable(DShop.TABLE);
            //dba.AddParam(DShop.CORP_ID, CbCompany.SelectedValue);
            dba.AddParam(DShop.CORP_ID, corp);
            dba.AddParam(DShop.NAME, shopName);
            dba.AddParam(DShop.ADDRESS, TbAddr.Text.Trim());
            dba.AddParam(DShop.LEVEL, CbLevel.SelectedValue);
            dba.AddParam(DShop.CONTACT, contact);
            dba.AddParam(DShop.POSTCODE, TbPostcode.Text.Trim());
            dba.AddParam(DShop.CELLPHONE, TbCellPhone.Text.Trim());
            dba.AddParam(DShop.TELEPHONE, TbTelephone.Text.Trim());
            dba.AddParam(DShop.STATUS, CkStatus.Checked ? CShop.STATUS_DISABLED : CShop.STATUE_ENABLED);
            dba.AddParam(DShop.UPDATE_TIME, DShop.NOW, false);
            if (isUpdate)
            {
                dba.AddWhere(DShop.ID, id);
                dba.ExecuteUpdate();

                Response.Redirect("~/POS/Shops.aspx?corp=" + CbCompany.SelectedValue);
            }
            else
            {
                id = HashUtil.UtcTimeInHex();
                dba.AddParam(DShop.ID, id);
                dba.AddParam(DShop.CODE, code);
                dba.AddParam(DShop.CREATE_TIME, DEmp.NOW, false);
                dba.ExecuteInsert();

                ShowDef_Last(CbCompany.SelectedValue, CbLevel.SelectedValue);
            }
        }

        private void ShowDef_Last(string company, string level)
        {
            HdId.Value = "";
            TbCode.Text = "";
            TbShopName.Text = "";
            TbAddr.Text = "";
            CbCompany.SelectedValue = company;
            CbLevel.SelectedValue = level;
            TbContact.Text = "";
            TbCellPhone.Text = "";
            TbTelephone.Text = "";
            TbPostcode.Text = "";
            CkStatus.Checked = false;
        }
        
        private void ShowDef()
        {
            HdId.Value = "";
            TbCode.Text = "";
            TbShopName.Text = "";
            TbAddr.Text = "";
            CbCompany.SelectedIndex = 0;
            CbLevel.SelectedIndex = 0;
            TbContact.Text = "";
            TbCellPhone.Text = "";
            TbTelephone.Text = "";
            CkStatus.Checked = false;
        }
    }
}